import pygame
import button
import pokemon
import csv
import move

SCREEN_WIDTH = 654
SCREEN_HEIGHT = 375

class StartGame():
    def __init__(self, screen):
        self.screen = screen
        self.pokedex = []
        self.pokemove = []
        self.movedex = []
        self.data=csv.reader(open('..\data\pokemon.csv'), delimiter=',', quotechar='/')
        for row in self.data:
            self.pokedex.append(row)
        self.movedata=csv.reader(open('..\data\pokemon_moves.csv'), delimiter=',', quotechar='/')
        for row in self.movedata:
            self.pokemove.append(row)
        self.move=csv.reader(open('..\data\moves.csv'), delimiter=',', quotechar='/')
        for row in self.move:
            self.movedex.append(row)
        print(self.pokemove)
        self.currentDexLookup = 1
        self.dispMode = 0
        self.textActive = False
        self.showPokeConfirmButton = False
        self.activeEntry = ""
        self.good = False
        self.chosen = 6
        self.activePokeSlot = None
        self.refresh = True
        self.pokeIDList = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
        self.mainPokeButtonList = []
        self.sideButtonList = []
        self.multiPokeRect = []
        self.sidePokeRect = []
        self.miscMultiPokeRect = []
        self.multiPokeRects = [[0, 320], [28, 265], [56, 210], [85, 155], [114, 101], [188, 46], [262, 101], [291, 155], [320, 210], [348, 265], [376, 320]]
        self.sidePokeRects = [[505, 7], [505, 70], [505, 133], [505, 195], [505, 258], [505, 321]]
        self.miscMultiPokeRects = [[188, 320], [200, 280]]
        
    def display(self):
        if(self.refresh):
            self.refresh = False
            self.screen.fill((255, 255, 255))
            self.dispSide()
            if(self.dispMode == 1):
                self.dispPokeSelect()
            if(self.dispMode == 2):
                self.drawAttackButtons()
        pygame.display.flip()
        pygame.time.delay(30)
    
    def dispSide(self):
        buttonText = []
        self.sideButtonList = []
        for pokeCount in range(0, 6):
            if(self.pokeIDList[pokeCount][0] == 0):
                buttonText.append("EMPTY")
            else:
                buttonText.append(self.pokedex[self.pokeIDList[pokeCount][0]][1])
        for n in range(0, 6):
            self.sideButtonList.append(button.PokeButton(buttonText[n]))
            self.sideButtonList[n].rect.x = self.sidePokeRects[n][0]
            self.sideButtonList[n].rect.y = self.sidePokeRects[n][1]
            self.screen.blit(self.sideButtonList[n].image, self.sidePokeRects[n])
        
    def dispPokeSelect(self):
        startNum = self.chosen - 5
        if(startNum < 1):
            startNum = 1
        endNum = self.chosen + 5
        if(endNum > 649):
            endNum = 649
        self.mainPokeButtonList = []
        self.miscMultiPokeRect = []
        self.multiPokeRect = []
 
        self.searchButton = button.SearchButton()
        self.searchButton.rect.x = self.miscMultiPokeRects[0][0]
        self.searchButton.rect.y = self.miscMultiPokeRects[0][1]
        self.miscMultiPokeRect.append(self.searchButton.rect)
        self.screen.blit(self.searchButton.image, self.miscMultiPokeRects[0])
        
        self.textButton = button.TextButton(self.activeEntry)
        self.textButton.rect.x = self.miscMultiPokeRects[1][0]
        self.textButton.rect.y = self.miscMultiPokeRects[1][1]
        self.miscMultiPokeRect.append(self.textButton.rect)
        self.screen.blit(self.textButton.image, self.miscMultiPokeRects[1])
        
        if(self.showPokeConfirmButton == True):
            tempButton = button.ArrowButton()
            tempButton.rect.x = 240
            tempButton.rect.y = 101
            self.showPokeConfirmButton = False
            print(self.showPokeConfirmButton)
            print(tempButton.rect)
            self.miscMultiPokeRect.append(tempButton.rect)
            self.screen.blit(tempButton.image, [tempButton.rect.x, tempButton.rect.y])
        
        for n in range(startNum, endNum + 1):
            self.mainPokeButtonList.append(button.PokeButton(self.pokedex[n][1]))
            self.mainPokeButtonList[-1].rect.x = self.multiPokeRects[n - self.chosen + 5][0]
            self.mainPokeButtonList[-1].rect.y = self.multiPokeRects[n - self.chosen + 5][1]
            self.multiPokeRect.append(self.mainPokeButtonList[-1].rect)
            self.screen.blit(self.mainPokeButtonList[-1].image, self.multiPokeRects[n - self.chosen + 5])
    
    def drawAttackButtons(self):
        if(self.miscMultiPokeRect != []):
            self.miscMultiPokeRect = []
            self.moveCount = 0
            self.pokeID = self.pokeIDList[self.activePokeSlot][0]
            self.currentMoveList = self.pokemove[self.pokeID]
            for n in self.currentMoveList:
                if(n != ""):
                    self.moveCount += 1
            self.currentPage = 1
            self.maxPages = self.moveCount // 20 + 1
        self.attackButtonRect = []
        self.otherButtonRect = []
        self.mainAttackRect = []
        for n in range(1, 5):
            tempButton = None
            if(self.pokeIDList[self.activePokeSlot][n] == 0):
                tempButton = button.AttackButton(1)
            else:
                print("specialDraw")
                tempButton = button.AttackButton(move.Move(int(self.pokeIDList[self.activePokeSlot][n])))
            tempButton.rect.x = (n - 1) * 125
            tempButton.rect.y = 0
            self.screen.blit(tempButton.image, [tempButton.rect.x, tempButton.rect.y])
            self.mainAttackRect.append(tempButton.rect)
                
        for n in range(0 + 20 * (self.currentPage - 1), 20 + 20 * (self.currentPage - 1)):
            if(self.currentMoveList[n] != ""):
                tempButton = button.AttackButton(move.Move(int(self.currentMoveList[n])))
                tempButton.rect.x = ((n % 20 )% 4) * 125
                tempButton.rect.y = 75 + ((n % 20) // 4) * 56
                self.screen.blit(tempButton.image, [tempButton.rect.x, tempButton.rect.y])
                self.attackButtonRect.append(tempButton.rect)
        font = pygame.font.SysFont("Arial", 16, False, False)
        text = font.render("Page " + str(self.currentPage) + " / " + str(self.maxPages), False, (0, 0, 0))
        self.screen.blit(text, [250 - text.get_size()[0] / 2, 357])
        if(self.currentPage < self.maxPages):
            tempButton = button.ArrowButton()
            tempButton.rect.x = 255 + text.get_size()[0] / 2
            tempButton.rect.y = 355
            self.screen.blit(tempButton.image, [tempButton.rect.x, tempButton.rect.y])
            self.otherButtonRect.append(tempButton.rect)
        if(self.currentPage > 1):
            tempButton = button.ArrowButton(False)
            tempButton.rect.x = 225 - text.get_size()[0] / 2
            tempButton.rect.y = 355
            self.screen.blit(tempButton.image, [tempButton.rect.x, tempButton.rect.y])
            self.otherButtonRect.append(tempButton.rect)
        
    
    def checkRects(self, point):
        for n in range(0, 6):
            if(self.sideButtonList[n].rect.collidepoint(point)):
                self.activePokeSlot = n
                if(self.pokeIDList[n][0] == 0):
                    self.dispMode = 1
                    self.refresh = True
                else:
                #elif(self.pokeIDList[n][1] == 0 or self.pokeIDList[n][2] == 0 or self.pokeIDList[n][3] == 0 or self.pokeIDList[n][4] == 0):
                    self.dispMode = 2
                    self.miscMultiPokeRect = 2
                    self.refresh = True
        if(self.dispMode == 1):
            collide = -1
            for n in range(0, len(self.miscMultiPokeRect)):
                if(self.miscMultiPokeRect[n].collidepoint(point)):
                    print("collide")
                    print(n)
                    collide = n
                    break
            if(collide == 0 and len(self.activeEntry) > 0):
                self.chosen = int(self.activeEntry)
                self.textActive = False
                self.refresh = True
            elif(collide == 1):
                self.activeEntry = ""
                self.textActive = True
                self.refresh = True
            elif(collide == 2):
                print("Confirmed")
                self.dispMode = 2
                self.pokeIDList[self.activePokeSlot][0] = self.chosen
                self.refresh = True
            else:
                self.showPokeConfirmButton = False
                self.refresh = True
                self.textActive = False
            collide = -1
            for n in self.multiPokeRect:
                if(n.collidepoint(point)):
                    collide = n
                    break
            if(type(collide) == pygame.Rect):
                self.pokeCircleAdjust(collide[0])
        elif(self.dispMode == 2):
            collided = -1
            for n in range(0, len(self.attackButtonRect)):
                if(self.attackButtonRect[n].collidepoint(point)):
                    collided = n
                    break
            if(collided != -1):
                self.addAttack(self.pokemove[self.pokeID][n + 20 * (self.currentPage - 1)])
                self.refresh = True
            collided = -1
            for n in range(0, len(self.mainAttackRect)):
                if(self.mainAttackRect[n].collidepoint(point)):
                    collided = n
                    break
            if(collided != -1):
                self.refresh = True
                self.pokeIDList[self.activePokeSlot][collided + 1] = 0
            for n in range(0, len(self.otherButtonRect)):
                if(self.otherButtonRect[n].collidepoint(point)):
                    if(self.otherButtonRect[n].x > 250):
                        self.currentPage += 1
                        self.refresh = True
                    else:
                        self.currentPage -= 1
                        self.refresh = True
                    
    def addAttack(self, attID):
        for n in range(1, 5):
            if(self.pokeIDList[self.activePokeSlot][n] == 0):
                self.pokeIDList[self.activePokeSlot][n] = attID
                break
        self.progressCheck()
        
    def progressCheck(self):
        self.good = True
        for i in range(0, 6):
            for j in range(0, 5):
                if(self.pokeIDList[i][j] == 0):
                    self.good = False
                
        
    def pokeCircleAdjust(self, x):
        if(x == 0):
            self.chosen -= 5
        elif(x == 28):
            self.chosen -= 4
        elif(x == 56):
            self.chosen -= 3
        elif(x == 85):
            self.chosen -= 2
        elif(x == 114):
            self.chosen -= 1
        elif(x == 188):
            self.showPokeConfirmButton = True
        elif(x == 262):
            self.chosen += 1
        elif(x == 291):
            self.chosen += 2
        elif(x == 320):
            self.chosen += 3
        elif(x == 348):
            self.chosen += 4
        elif(x == 376):
            self.chosen += 5
        self.refresh = True
    
    def run(self):
        while 1:
            if(self.good):
                return self.pokeIDList
            self.display()
            for event in pygame.event.get():
                if(event.type == pygame.MOUSEBUTTONDOWN):
                    self.checkRects(event.pos)
                elif(event.type == pygame.KEYDOWN):
                    self.refresh = True
                    if(self.textActive == True and (event.key == pygame.K_0 or event.key == pygame.K_KP0)):
                        self.activeEntry += "0"
                    elif(self.textActive == True and (event.key == pygame.K_1 or event.key == pygame.K_KP1)):
                        self.activeEntry += "1"
                    elif(self.textActive == True and (event.key == pygame.K_2 or event.key == pygame.K_KP2)):
                        self.activeEntry += "2"
                    elif(self.textActive == True and (event.key == pygame.K_3 or event.key == pygame.K_KP3)):
                        self.activeEntry += "3"
                    elif(self.textActive == True and (event.key == pygame.K_4 or event.key == pygame.K_KP4)):
                        self.activeEntry += "4"
                    elif(self.textActive == True and (event.key == pygame.K_5 or event.key == pygame.K_KP5)):
                        self.activeEntry += "5"
                    elif(self.textActive == True and (event.key == pygame.K_6 or event.key == pygame.K_KP6)):
                        self.activeEntry += "6"
                    elif(self.textActive == True and (event.key == pygame.K_7 or event.key == pygame.K_KP7)):
                        self.activeEntry += "7"
                    elif(self.textActive == True and (event.key == pygame.K_8 or event.key == pygame.K_KP8)):
                        self.activeEntry += "8"
                    elif(self.textActive == True and (event.key == pygame.K_9 or event.key == pygame.K_KP9)):
                        self.activeEntry += "9"
                    elif(self.textActive == True and (event.key == pygame.K_BACKSPACE)):
                        if(len(self.activeEntry) >= 1):
                            self.activeEntry = self.activeEntry[0:len(self.activeEntry)-1]
                elif(event.type == pygame.QUIT):
                    pygame.quit()
                    exit()
    